<template lang="pug">
  el-dialog(
    title="人员信息"
    :visible="visibleDialog"
    width="720px"
    center
    @close="visibleDialog = false"
  )
    el-row(:gutter="24")
      el-col(:span="18")
        form-box(:items="items" :showBtns="false" labelWidth="100px" :layoutCol="{sm: 24, md: 24, lg: 24}")
      el-col(:span="6")
        a-image(:url="BASE_URL + 'file/download/' + info.photoUrl + '?_csrf=' + _csrf" height="160px")
    s-table(
      ref="table"
      v-if="id && tmplType"
      :column="column"
      :queryFn="getList"
      :tableProps="{size: 'mini'}"
      :pageSize="5"
      :queryPropsParams="{psnNo: id, tmplType: tmplType}"
      :pagination="null"
      rowKey="tmplId"
      )
      template(slot="tmplType" slot-scope="text, scope") {{dictFilter(tmplType, 'SCEN_BOSG_TYPE_MAJCLS')}}
      template(slot="valiFlag" slot-scope="text, scope") {{dictFilter(text, 'VALI_FLAG')}}
      template(slot="action" slot-scope="text, scope")
        el-popconfirm(title="确定修改该模板状态吗吗？" @confirm="statusHandle(scope)" @onConfirm="statusHandle(scope)")
          el-link.mr5(type="warning" :underline="false" slot="reference") {{scope.valiFlag === '1' ? '禁用' : '启用'}}
        el-popconfirm(title="确定删除该模板吗？" @confirm="deleteHandle(scope)" @onConfirm="deleteHandle(scope)")
          el-link(type="danger" :underline="false" slot="reference") 删除
    p.tr(slot="footer")
      el-button(type="danger") 回退
      el-button(type="warning") 修复
      el-button(type="primary") 矫正
</template>
<script>
import { STable, FormBox, AImage } from '@/components'
import { biologyInfo, modelListByUser, biologyAdd, modelDelByUser, modelUpdateByUser } from '@/api/libary'

const column = [
  {
    prop: 'tmplId',
    label: '模板编码',
    align: 'center'
  },
  {
    prop: 'tmplType',
    label: '建模方式',
    align: 'center'
  },
  {
    prop: 'crteTime',
    label: '建模时间',
    align: 'center'
  },
  {
    prop: 'valiFlag',
    label: '状态',
    align: 'center'
  },
  {
    prop: 'action',
    label: '操作',
    width: '120',
    align: 'center'
  }
]

export default {
  components: {
    FormBox,
    STable,
    AImage
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    visibleDialog: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', val)
      }
    },
    items () {
      const { info } = this
      return [
        {
          key: 'psnName',
          label: '姓名',
          type: 'text',
          value: info.psnName
        },
        {
          key: 'certno',
          label: '身份证',
          type: 'text',
          value: info.certno
        },
        {
          key: 'bioType',
          label: '生物类型',
          type: 'radiobutton',
          initialValue: this.tmplType,
          changeHandle: val => { this.tmplType = val },
          options: this.dict.SCEN_BOSG_TYPE_MAJCLS
        },
        {
          key: 'file',
          label: ' ',
          type: 'upload',
          accept: 'image/*',
          canRepeat: true,
          action: this.BASE_URL + 'file/upload',
          'show-file-list': false,
          success: this.fileChange
        }
      ]
    }
  },
  watch: {
    visible: {
      handler (val) {
        if (val) this.getInfo()
      }
    },
    tmplType: {
      handler () {
        this.$nextTick(function () {
          this.refreshTable(true)
        })
      }
    }
  },
  data () {
    return {
      column,
      tmplType: '',
      getList: modelListByUser,
      info: {}
    }
  },
  methods: {
    getInfo () {
      biologyInfo(this.id).then(res => {
        this.info = res
        if (this.tmplType) {
          this.refreshTable(true)
        } else {
          this.tmplType = this.dict.SCEN_BOSG_TYPE_MAJCLS && this.dict.SCEN_BOSG_TYPE_MAJCLS[0] ? this.dict.SCEN_BOSG_TYPE_MAJCLS[0].value : ''
        }
      })
    },
    // 刷新表格
    refreshTable (flag) {
      this.$refs.table.refresh(flag)
    },
    // 修改模板状态
    statusHandle (obj) {
      modelUpdateByUser({ tmplStas: obj.valiFlag === '1' ? 0 : 1, tmplId: obj.tmplId, tmplType: this.tmplType }).then(() => {
        this.$message.success('修改模板状态成功')
        this.refreshTable(true)
      })
    },
    // 删除模板
    deleteHandle (obj) {
      modelDelByUser({ tmplId: obj.tmplId, tmplType: this.tmplType }).then(() => {
        this.$message.success('删除成功')
        this.getInfo()
        this.refreshTable(true)
      })
    },
    fileChange ({ file }) {
      if (file.response && file.response.code === 0) {
        biologyAdd({ psnNo: this.id, photoUrl: file.response.data, tmplType: this.tmplType }).then(() => {
          this.$message.success('上传成功')
          this.getInfo()
          this.refreshTable(true)
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.warningBox{
  padding: 12px;
  p {
    line-height: 1.8
  }
}
</style>
